/*
 * Copyright 1997-2013
 *
 * http://www.pconline.com.cn
 *
 */
package cn.pconline.autoclub.util.ubb.node;

import org.gelivable.web.EnvUtils;

import cn.pconline.autoclub.util.ubb.EscapeUtils;
import cn.pconline.autoclub.util.ubb.Node;

/**
 *
 * @author xhchen
 */
public class ImgNodeWap extends AbstractNode {

    private String thumbNailSize;

	public ImgNodeWap() {}

	private ImgNodeWap(String name, String attribute) {
		super.name = name;
		super.attribute = attribute;
	}

    public void setThumbNailSize(String thumbNailSize) {
        this.thumbNailSize = thumbNailSize;
    }

    @Override
	public StringBuilder getText(java.util.LinkedList<Node> stack) {
		StringBuilder sb = new StringBuilder();
        String muri = text.toString();

        if (muri.indexOf("editor/gotoquote.gif") != -1) {
            return sb;
        }

        if (thumbNailFlag && thumbNailSize != null) {
        	muri = muri.replace("1024x1024it", thumbNailSize);
            muri = muri.replace("1024x1024", thumbNailSize);
        }

        boolean flag = EnvUtils.getEnv().getRequest().getAttribute("wapHadImgFlag") == null;
        sb.append("<div class=\"viewBigPic\">\n<img src2=\"").append(muri).append("\" src=\"http://www1.pcauto.com.cn/wap/2013/cms/img/loading.png\"/>\n</div>");

        if (flag) {
            EnvUtils.getEnv().getRequest().setAttribute("wapHadImgFlag", 1);
        }

		return sb;
	}

	@Override
	public void appendText(CharSequence text) {
		if (attribute == null) {
			EscapeUtils.appendAttribute(text, super.text);
		} else {
			EscapeUtils.appendHtml(text, super.text);
		}
	}

	@Override
	public Node copyWithoutText() {
        ImgNodeWap imgNodeWap = new ImgNodeWap(name, attribute);
        imgNodeWap.setThumbNailSize(thumbNailSize);
        return imgNodeWap;
	}

	@Override
	public Node build(String name, String attribute) {
        ImgNodeWap imgNodeWap = new ImgNodeWap(name, attribute);
        imgNodeWap.setThumbNailSize(thumbNailSize);
		return imgNodeWap;
	}
}
